package com.example.studentdemo.service.impl;

import com.example.studentdemo.constant.RoleConstant;
import com.example.studentdemo.entity.Account;
import com.example.studentdemo.entity.Subject;
import com.example.studentdemo.exception.CustomerException;
import com.example.studentdemo.mapper.SubjectMapper;
import com.example.studentdemo.service.SubjectService;
import com.example.studentdemo.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {
    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public List<Subject> selectAll(Subject subject) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            throw new CustomerException("未登录");
        }

        // 根据用户角色添加查询条件
        String role = currentUser.getRole();
        if ("TEACHER".equals(role)) {
            // 教师只能查看自己创建的课程
            subject.setTeacherId(currentUser.getId());
        } else if ("STUDENT".equals(role)) {
            // 学生只能查看自己参加的课程
            return subjectMapper.selectByStudentId(currentUser.getId());
        }
        // 管理员没有限制
        return subjectMapper.selectAll(subject);
    }

    @Override
    public void add(Subject subject) {
        // 检查课程名称是否已存在
        Subject dbSubject = subjectMapper.selectByName(subject.getName());
        if (dbSubject != null) {
            throw new CustomerException("课程名称已存在");
        }
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser != null && currentUser.getRole().equals(RoleConstant.TEACHER_ROLE)) {
            subject.setTeacherId(currentUser.getId());
        }
        subjectMapper.insert(subject);
    }

    @Override
    public void update(Subject subject) {
        // 检查是否存在该课程
        Subject dbSubject = subjectMapper.selectById(subject.getId());
        if (dbSubject == null) {
            throw new CustomerException("课程不存在");
        }
        // 检查课程名称是否已被其他课程使用
        Subject dbSubjectByName = subjectMapper.selectByName(subject.getName());
        if (dbSubjectByName != null && !dbSubjectByName.getId().equals(subject.getId())) {
            throw new CustomerException("课程名称已被使用");
        }
        subjectMapper.updateById(subject);
    }

    @Override
    public void deleteById(Integer id) {
        subjectMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Subject> subjectList) {
        for (Subject subject : subjectList) {
            this.deleteById(subject.getId());
        }
    }

    @Override
    public PageInfo<Subject> selectPage(Integer pageNum, Integer pageSize, Subject subject) {
        // 获取当前登录用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (currentUser == null) {
            throw new CustomerException("未登录");
        }

        // 根据用户角色添加查询条件
        String role = currentUser.getRole();
        if ("TEACHER".equals(role)) {
            // 教师只能查看自己创建的课程
            subject.setTeacherId(currentUser.getId());
            // 开启分页查询
            PageHelper.startPage(pageNum, pageSize);
            List<Subject> subjectList = subjectMapper.selectAll(subject);
            return PageInfo.of(subjectList);
        } else if ("STUDENT".equals(role)) {
            // 学生只能查看自己参加的课程
            PageHelper.startPage(pageNum, pageSize);
            List<Subject> subjectList = subjectMapper.selectByStudentId(currentUser.getId());
            return PageInfo.of(subjectList);
        }
        // 管理员没有限制
        PageHelper.startPage(pageNum, pageSize);
        List<Subject> subjectList = subjectMapper.selectAll(subject);
        return PageInfo.of(subjectList);
    }

    @Override
    public Subject selectById(Integer id) {
        return subjectMapper.selectById(id);
    }
}